package artist.graphic.cn.http;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/12/25.
 */

public class GetImageFromNet {




    private Bitmap bitmap;

    public static  Bitmap getImage(String mUrl) {

        try {
            URL url = new URL(mUrl);


            //获取连接对象
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            //请求方式
            httpURLConnection.setRequestMethod("GET");

            //请求超时时间设置
            httpURLConnection.setReadTimeout(10000);

            //读取超时
            httpURLConnection.setReadTimeout(10000);

            //请求连接
            httpURLConnection.connect();


            //如果响应码为200说明请求成功
            if (httpURLConnection.getResponseCode() == 200) {

                //获得服务器响应流，流里面的数据就是可以请求的
                InputStream is = httpURLConnection.getInputStream();

                //通过流读取数据，用构造场来转化图片
                Bitmap bm = BitmapFactory.decodeStream(is);

                return bm;



            } else {
//                Toast.makeText(mContext, "请求失败", Toast.LENGTH_SHORT).show();
                //Toast.makeText(this,"请求失败",1).show();
            }

        } catch (Exception e) {
//            Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();

            System.out.println(e.toString());
        }

        return  null;

    }


    public Bitmap getBitmap() {
        return bitmap;
    }

    public Bitmap getBitmap(String url) {
        return getImage(url);
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }




}
